<?php

	function admin()
	{
		if ($_SESSION['current_user']['id'] == -1) return true; //the primary admin user can do anything!
  	elseif (is_array($_SESSION['current_user']['user_roles']) && array_key_exists('-1', $_SESSION['current_user']['user_roles'])) return true;//super users can do anything!
		
		return false;
	}
	
	

  function array_flatten($array, $flat = false)
	{
		if (empty($flat)) $flat = array();
		
  	foreach ($array as $key => $val) {
			if (is_array($val)) $flat = array_flatten($val, $flat);
			else $flat[] = $val;
      
		}
		
    #print_r($flat);
		return $flat; 
  }
	
	
	
	function array_stripslashes($array)
	{
	if(!is_array($array) || count($array) == 0) return $array;			
		foreach ($array as $key => $val) {
			$array[$key] = is_array($val) ? array_stripslashes($val) : stripslashes($val);
		}
		
		return $array;
	}


	
	function authorized($table_or_module = false, $action_or_file = false, $check_for_data_access = false) {
		//echo "$table_or_module/$action_or_file/$check_for_data_access<br />";
		
		//prevent deleting of conf
		if ($table_or_module == 'conf' && $action_or_file == 'delete') return false;
		
		if (admin()) return true;
		
  	#select,insert,update,delete,archive are reserved for table perms (no module can be)
    return ($check_for_data_access)
			? $_SESSION['current_user']['data_access'][$table_or_module][$action_or_file] === true
			: $_SESSION['current_user']['module_access'][$table_or_module][$action_or_file] === true;
  }
  